﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace NNTH
{
    public partial class fXepPhongThi : DevExpress.XtraEditors.XtraForm
    {
        public fXepPhongThi()
        {
            InitializeComponent();
        }

        Data.QLTTNNTHEntities db = new Data.QLTTNNTHEntities();
        private void fXepPhongThi_Load(object sender, EventArgs e)
        {
            spinEdit2.Value = DateTime.Now.Year;
            spinEdit1.EditValue = 100;
            textEdit1.EditValue = "SBD";
            dateEdit1.EditValue = DateTime.Now;
            dateEdit2.EditValue = DateTime.Now.AddDays(7);
            monHocBindingSource.DataSource = db.MonHoc;
            phongHocBindingSource.DataSource = db.PhongHoc;
        }

        private void gridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUpEdit1.EditValue != null)
            {
                var MH = (int)gridLookUpEdit1.EditValue;
                var nam = spinEdit2.Value;
                var k = db.KeHoachHT.Where(p => p.MaMH == MH && p.NamHoc ==nam).ToList() ;
                var klis= (from x in k
                          select x.MaLop);

                var l = from p in db.LopHoc
                        where klis.Contains(p.MaLop)
                        select p;
                lopHocBindingSource.DataSource = l;
            }
            else
            {
                lopHocBindingSource.DataSource = typeof(Data.LopHoc);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
            {


                if (gridLookUpEdit1.EditValue != null)
                {
                    var MH = (int)gridLookUpEdit1.EditValue;
                    var nam = spinEdit2.Value;
                    var k = db.KeHoachHT.Where(p => p.MaMH == MH && p.NamHoc == nam).ToList();
                    var klis = (from x in k
                                select x.MaLop);

                    foreach (var kht in k)
                    {
                        var KH = (Data.KeHoachHT)kht;
                        
                        var listHV = db.HocVien.Where(p =>p.MaLop ==KH.MaLop).ToList();
                        foreach (var item in listHV)
                        {
                            if (!db.BangDiem.Where(p => p.MaHV == item.MaHV && p.KHHTID == KH.ID).Any())
                            {
                                var BD = new Data.BangDiem()
                                {
                                    KHHTID = KH.ID,
                                    MaHV = item.MaHV,
                                    Diem1 = 0,
                                    Diem2 = 0,
                                    Thi1= (DateTime)dateEdit1.EditValue,
                                    Thi2 = (DateTime)dateEdit2.EditValue
                                };
                                db.BangDiem.AddObject(BD);
                            }
                        }
                    }
                    db.SaveChanges();

                    var khhtList = (from x in k
                                    select x.ID);
                    bangDiemBindingSource.DataSource = db.BangDiem.Where(p => khhtList.Contains(p.KHHTID)).OrderBy(p => p.HocVien.Ten);
                }
            }
            else
            {
                MessageBox.Show("Chọn ngày thi trước khi lấy danh sách xếp lịch thi");
            }
        }

        private void spinEdit1_EditValueChanged(object sender, EventArgs e)
        {
            string f = "D";
            if (spinEdit3.Value!=0)
            {
                f+=spinEdit3.Value.ToString();
            }
            else
            {
                f += "0";
            }
            labelControl1.Text = "VD: " + textEdit1.EditValue.ToString() + ((int)(spinEdit1.Value)).ToString(f);
        }


        private void XepPhong_Click(object sender, EventArgs e)
        {
            if (bangDiemBindingSource.Current!=null)
            {
                var p = phongHocBindingSource.DataSource as IEnumerable<Data.PhongHoc>;
                if (p.Where(t=>t.Chon).Any())
                {
                    var phong = p.Where(t => t.Chon);
                    
                    if ((bool)radioGroup1.EditValue)//Chia đều
                    {
                       
                        var db = bangDiemBindingSource.DataSource as IEnumerable<Data.BangDiem>;
                        int tongSV = db.Count();
                        int TongSucChua = phong.Sum(t=>t.SucChua);
                        int i = 0;
                        int k = 1;
                        foreach (var item in db)
                        {
                            i++;
                            if (i < phong.Page(k, 1).Sum(m =>( m.SucChua * tongSV)/TongSucChua))
                            {
                                item.PhongHoc = phong.Page(k, 1).First();
                            }
                            else
                            {
                                k++;
                            }
                        }
                    }
                    else
                    {
                        var db = bangDiemBindingSource.DataSource as IEnumerable<Data.BangDiem>;
                        int i = 0;
                        int k=1;
                        foreach (var item in db)
                        {
                            i++;
                            if (i < phong.Page(k,1).Sum(m=>m.SucChua))
                            {
                                item.PhongHoc = phong.Page(k, 1).First();
                            }
                            else
                            {
                                k++;
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Xin chọn phòng để xếp lịch thi");
                }
            }
        }


        private void DanhSOBD_Click(object sender, EventArgs e)
        {
            if (bangDiemBindingSource.Current != null)
            {
                var l = bangDiemBindingSource.DataSource as IEnumerable<Data.BangDiem>;
                int i = (int)spinEdit1.Value;
                foreach (var item in l)
                {
                    string f = "D";
                    if (spinEdit3.Value != 0)
                    {
                        f += spinEdit3.Value.ToString();
                    }
                    else
                    {
                        f += "0";
                    }
                   
                    item.SoBD = textEdit1.EditValue.ToString() + ((int)(i)).ToString(f);
                    i++;
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.bangDiemBindingSource.Current != null)
            {
                var report = new Reports.rePhieuDuThi();
                BindingSource bs = (BindingSource)report.DataSource;
                bs.DataSource = this.bangDiemBindingSource.List;
                report.ShowPreviewDialog();
            }
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }
    }
    public static class PagingExtensions
    {
        //used by LINQ to SQL
        public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
        {
            return source.Skip((page - 1) * pageSize).Take(pageSize);
        }

        //used by LINQ
        public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
        {
            return source.Skip((page - 1) * pageSize).Take(pageSize);
        }

    }
}